package com.github.prontera.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.domain.Order;
import com.github.prontera.persistence.CrudMapper;
import com.github.prontera.persistence.OrderMapper;

/**
 * 全部订单
 */
@Service
public class OrderService extends CrudServiceImpl<Order>{
    
    @Autowired
    private OrderMapper mapper;
    
    private static Object lock=new Object();
    
  	@Autowired
    public OrderService(CrudMapper<Order> mapper) {
        super(mapper);
    }

	public Integer updateByTradeNo(Order order) {
		return mapper.updateByTradeNo(order);
	}

	public Long orderCount() {
		return mapper.orderCount();
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void insertOrder(Order order) {
//		if(order.getServiceId()==null){
//			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"serviceId比传");
//		}
		if(order.getClientId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"clientId比传");
		}
		if(order.getType()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"type比传");
		}
		synchronized(lock){
			Order findOrder=new Order();
			findOrder.setClientId(order.getClientId());
			findOrder.setType(order.getType());
			findOrder.setServiceId(order.getServiceId());
//			long count=mapper.count(findOrder);
//			if(count>0){
//				Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"已经存在该订单!");
//			}
			if(mapper.insert(order)<=0){
				Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"参数错误,请重试!");
			}
		}
	}

}
